%综合大小信噪比下的遍历容量

clear
clc
NUM_timesamples = 1000;
tilde_h = (randn(NUM_timesamples)+ j*randn(NUM_timesamples))/sqrt(2);
Pathloss = 1;
h = sqrt(Pathloss)*tilde_h;
SNR_dB = -10:5:30;

% 遍历容量
for I_SNR = 1: length(SNR_dB)
    rho = 10^(SNR_dB(I_SNR)/10);
    for t = 1:NUM_timesamples
    Capacity_temp(t) = log2(abs(1+rho*h(t)*h(t)') );   
    end
    Capacity(I_SNR)= sum(Capacity_temp)/NUM_timesamples; 
end

% 大信噪比
for I_SNR = 1: length(SNR_dB)
    rho = 10^(SNR_dB(I_SNR)/10);
    for t = 1:NUM_timesamples
    Capacity_temp(t) = log2(abs(rho*h(t)*h(t)') );
    end
    Capacity_high(I_SNR)= sum(Capacity_temp)/NUM_timesamples;
end

% 小信噪比
for I_SNR = 1: length(SNR_dB)
    rho = 10^(SNR_dB(I_SNR)/10);
    
    for t = 1:NUM_timesamples
    h_temp(t) = h(t)*h(t)';
    end
    h_average= sum(h_temp)/NUM_timesamples;

    Capacity_low(I_SNR)=rho*h_average*log2(exp(1));
end

% 上界
for I_SNR = 1: length(SNR_dB)
    rho = 10^(SNR_dB(I_SNR)/10);
    
    for t = 1:NUM_timesamples
    h_temp(t) = h(t)*h(t)';
    end
    h_average= sum(h_temp)/NUM_timesamples;
    Upper_bound(I_SNR)=log2(abs(1+rho*h_average));
end

% 分析结果
for I_SNR = 1: length(SNR_dB)
    rho = 10^(SNR_dB(I_SNR)/10);
    for t = 1:NUM_timesamples
    Ana_result_temp(t) = log2(abs(1+rho*h(t)*h(t)') );   
    end
    Analytical_result(I_SNR)= sum(Ana_result_temp)/NUM_timesamples; 
end

subplot(2, 1, 1); % 第一个子图
hold on
plot(SNR_dB,Capacity, 'ro-')
plot(SNR_dB,Capacity_high, 'bo-')
plot(SNR_dB,Analytical_result, 'go')
plot(SNR_dB,Upper_bound, 'co-')

legend('Ergodic Capacity','Capacity(high)','Analytical(result)','Upper bound', 'Location', 'NorthWest'),
title('多个函数与信噪比的关系(路径损耗=1)');
xlabel('\rho (in dB)')
ylabel('Ergodic Capacity (bps/Hz)')
grid


Pathloss = 5;
h = sqrt(Pathloss)*tilde_h;
SNR_dB = -10:5:30;

% 遍历容量
for I_SNR = 1: length(SNR_dB)
    rho = 10^(SNR_dB(I_SNR)/10);
    for t = 1:NUM_timesamples
    Capacity_temp(t) = log2(abs(1+rho*h(t)*h(t)') );   
    end
    Capacity(I_SNR)= sum(Capacity_temp)/NUM_timesamples; 
end

% 大信噪比
for I_SNR = 1: length(SNR_dB)
    rho = 10^(SNR_dB(I_SNR)/10);
    for t = 1:NUM_timesamples
    Capacity_temp(t) = log2(abs(rho*h(t)*h(t)') );
    end
    Capacity_high(I_SNR)= sum(Capacity_temp)/NUM_timesamples;
end

% 小信噪比
for I_SNR = 1: length(SNR_dB)
    rho = 10^(SNR_dB(I_SNR)/10);
    
    for t = 1:NUM_timesamples
    h_temp(t) = h(t)*h(t)';
    end
    h_average= sum(h_temp)/NUM_timesamples;

    Capacity_low(I_SNR)=rho*h_average*log2(exp(1));
end

% 上界
for I_SNR = 1: length(SNR_dB)
    rho = 10^(SNR_dB(I_SNR)/10);
    
    for t = 1:NUM_timesamples
    h_temp(t) = h(t)*h(t)';
    end
    h_average= sum(h_temp)/NUM_timesamples;
    Upper_bound(I_SNR)=log2(abs(1+rho*h_average));
end

% 分析结果
for I_SNR = 1: length(SNR_dB)
    rho = 10^(SNR_dB(I_SNR)/10);
    for t = 1:NUM_timesamples
    Ana_result_temp(t) = log2(abs(1+rho*h(t)*h(t)') );   
    end
    Analytical_result(I_SNR)= sum(Ana_result_temp)/NUM_timesamples; 
end


subplot(2, 1, 2); % 第一个子图
hold on
plot(SNR_dB,Capacity, 'ro-')
plot(SNR_dB,Capacity_high, 'bo-')
plot(SNR_dB,Analytical_result, 'go-')
plot(SNR_dB,Upper_bound, 'co-')

legend('Ergodic Capacity','Capacity(high)','Analytical(result)','Upper bound', 'Location', 'NorthWest'),
title('多个函数与信噪比的关系(路径损耗=5)');
xlabel('\rho (in dB)')
ylabel('Ergodic Capacity (bps/Hz)')
grid